<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
        <el-form-item label="登记日期" prop="name">
          <el-date-picker
            v-model="queryParams.taskDate"
            size="mini"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>

        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  

      <el-table v-loading="loading" :data="deployList">
        <!-- <el-table-column label="编号" prop="id" align="center" /> -->
        <el-table-column label="班级" align="center" prop="className" :show-overflow-tooltip="true" />
        <el-table-column label="班主任" align="center" prop="registrant" :show-overflow-tooltip="true" />
        <el-table-column label="班级人数" align="center" prop="studentNumber" :show-overflow-tooltip="true" />
        <el-table-column label="登记情况" align="center" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            <!-- <div v-html="scope.row.registrantDesc" style="color:red"></div> -->
             <div v-if="!scope.row.knowledgePoints" style="color:red">重要知识点未登记</div>
             <div v-if="scope.row.knowledgePoints" style="color:green">重要知识点已登记</div>
             <div v-if="!scope.row.syntax" style="color:red">语法未登记</div>
             <div v-if="scope.row.syntax" style="color:green">语法已登记</div>
             <div v-if="!scope.row.word" style="color:red">单词未登记</div>
             <div v-if="scope.row.word" style="color:green">单词已登记</div>
             <div v-if="!scope.row.schoolAsignment" style="color:red">作业未登记</div>
             <div v-if="scope.row.schoolAsignment" style="color:green">作业已登记</div>
             <!-- <div v-if="scope.row.schoolAsignment && scope.row.syntax && scope.row.word && scope.row.schoolAsignment" style="color:green">已登记</div> -->
          </template>
        </el-table-column>
        <!-- <el-table-column label="考核通过比率" align="center" prop="recitePassRatioList" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            <p v-for="item in scope.row.recitePassRatioList" :key = "item"><span >{{ item|| 0 }}%</span></p>
          </template>
        </el-table-column>
        <el-table-column label="作业完成比率" align="center" prop="workCheckRatioList" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            <p v-for="item in scope.row.workCheckRatioList" :key = "item"><span >{{ item|| 0 }}%</span></p>
          </template>
        </el-table-column> -->

        <el-table-column label="登记日期" align="center" prop="taskDate" fixed="left" width="180" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            <span  v-if="scope.row.taskDate==parseTime(new Date()).substring(0,10)" style="font-weight: 700; color: green;">{{ scope.row.taskDate }}</span>
            <span  v-else style="color:burlywood">{{ scope.row.taskDate }}</span>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          align="center"
          width="230">
          <template slot-scope="scope">
            <el-button @click="detailShow(scope.row)" type="text" size="small">查看</el-button>
            <el-button type="text" size="small" @click="showWork(scope.row)" v-if="scope.row.taskDate==parseTime(new Date()).substring(0,10)">登记</el-button>
            <!-- <el-button @click="checkShow(scope.row)" type="text" size="small">考核登记</el-button>
            <el-button @click="workShow(scope.row)" type="text" size="small">作业登记</el-button> -->
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <!--任务提交-->
      <el-dialog
        title="登记"
        :visible.sync="dialogVisible"
        v-if="dialogVisible"
        append-to-body
        width="60%"
        :before-close="handleClose">
        <dayEdit :wordId="wordId" @handleClose="handleClose"></dayEdit>
      </el-dialog>
      <!--考核登记-->
      <el-dialog
        title="登记"
        :visible.sync="checkDialogVisible"
        v-if="checkDialogVisible"
        append-to-body
        width="60%"
        :before-close="handleWorkClose">
        <el-divider content-position="left">作业</el-divider>
        
        <el-table :data="reciteStudentList" prop="examStudents" height="450" 
            border  @selection-change="handleSelectionChange" >
              <el-table-column align="center"
                type="selection" :selectable="isRowSelectable"
                width="55">
              </el-table-column>
              <el-table-column label="编号" align="center" width="80" prop="studentId" />
              <el-table-column width="100"
                label="学生姓名"
                align="center"
                prop="studentName"
              />
              <el-table-column label="考核次数" align="center" width="120" prop="isPass">
                <template slot-scope="scope">
                  <el-select v-model="scope.row.doneNum" placeholder="">
                    <el-option v-for="i in 6" :key="i" :value="i" :label="'第'+i+'次'"></el-option>
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column label="是否完成" align="center" prop="isPass">
                <template slot-scope="scope">
                  <el-radio v-model="scope.row.checkState" label="0">未完成</el-radio>
                  <el-radio v-model="scope.row.checkState" label="1">完成</el-radio>
                </template>
              </el-table-column>
              <el-table-column label="备注" align="center" prop="remarks">
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.remarks"
                    placeholder="备注"
                  ></el-input>
                </template>
              </el-table-column>
              <!-- <el-table-column
                fixed="right"
                label="操作"
                align="center"
                width="80">
                <template slot-scope="scope">
                  <el-button @click="helpShow(scope.row)" type="text" size="small">帮扶</el-button>

                </template>
              </el-table-column> -->
        </el-table>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitCheckStuList">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog
        title="详情" append-to-body
        :visible.sync="detailDialogVisible"
        v-if="detailDialogVisible"
        width="60%" 
        :before-close="handleCloseDetail">
        <dayDetail :wordId="wordId" :row="selectRow" @handleClose="handleCloseDetail" :type="1"></dayDetail>
      </el-dialog>

      <el-dialog
        title="提示" append-to-body
        :visible.sync="helpDialogVisible"
        v-if="helpDialogVisible"
        width="30%"
        :before-close="handleCloseHelp"
        >
        <classStudentList :row="selectRow" @handleCloseHelp="handleCloseHelp"></classStudentList>
        
      </el-dialog>

    </div>
  </template>
  
  <script>
  import { getToken } from "@/utils/auth";
  import {pageList,checkStudentList, addCheckResult} from "@/api/evaluation/day"
  import dayEdit from "@/views/evaluation/day/edit"
  import {queryClassStudents} from "@/api/exam/examResultEnter";
  import dayDetail from "@/views/evaluation/day/detail"
  
  import classStudentList from "@/views/evaluation/help/classStudentList"

  export default {
    name: "dayList",
    components: {
      dayEdit,dayDetail,classStudentList
    },
    props:{
      done:{
        type:String,
        default:null
      },
      classId:{
        type:String,
        default:null
      }
    },
    data() {
      return {
        helpDialogVisible:false,
        detailDialogVisible:false,
        wordId:0,
        dialogVisible:false,
        uploadProcessPath:process.env.VUE_APP_BASE_API + '/workflow/uploadProcessZip',
        headers: { Authorization: "Bearer " + getToken() },
        fileList:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 参数表格数据
        deployList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 类型数据字典
        typeOptions: [],
        // 日期范围
        dateRange: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          done:'99',
          paramName: undefined,
          paramKey: undefined,
          paramType: undefined
        },
        today:null,
        tableTitle:[],
        reciteStudentList:[],
        checkDialogVisible:false,
        multipleSelection:[],
        //选中学生列表
        selectStuList:[],
        checkType:1,
        selectRow:{}
      };
    },

    created() {
      this.getList();
      if(this.done){
        this.queryParams.done = this.done
        this.queryParams.classId = Number(this.classId)
      }
  
    },
    methods: {
      /**关闭帮扶窗口 */
      handleCloseHelp(){
        this.helpDialogVisible = false
      },
      /**打开帮扶选择窗口 */
      helpShow(row){
        this.selectRow = row
        this.helpDialogVisible= true
      },
      /**添加帮扶学生 */
      submitHelpStudent(){
        addHelp().then(resp=>{

        })
      },
      /**添加多选禁用 */
      isRowSelectable(row, index){
        console.log(row.studentId)
        console.log(this.selectStuList.length)
        for(var i=0;i<this.selectStuList.length;i++){
          console.log("--------------"+this.selectStuList[i].studentId)
          if(this.selectStuList[i].studentId==row.studentId){
            return false
          }
        }
        return true
      },
      /**提交选中学生 */
      submitCheckStuList(){
        if(this.multipleSelection==null || this.multipleSelection.length<=0){
          this.$message({
            message: '请选择一条记录',
            type: 'warning'
          });
          return
        }
        addCheckResult(this.multipleSelection).then(resp=>{
          if(resp.code==200){
            this.msgSuccess("操作成功")
            this.checkDialogVisible = false
          }
        })
      },
      /**查询选中学生 */
      async selectCheckStuList(row){
        this.selectStuList=[]
        checkStudentList({
          workDayId:row.id,
          checkType:this.checkType
        }).then(resp=>{
          this.selectStuList = resp.data
          this.getClassStudent(row)
          
        })
      },
      /**table多选 */
      handleSelectionChange(val) {
        this.multipleSelection = val;
        console.log(val)
      },
      detailShow(row){
        this.detailDialogVisible=true
        this.wordId = row.id
        this.selectRow = row
      },
      workShow(row){
        this.checkDialogVisible = true
        this.checkType = 2
        this.selectRow = row
        this.selectCheckStuList(row)
      },
      /**打开考核页面 */
      checkShow(row){
        this.checkDialogVisible = true
        this.checkType = 1
        this.selectCheckStuList(row)
        // this.getClassStudent(row)
        
        console.log(this.reciteStudentList)
        console.log(this.selectStuList)
        
      },
      //通过班级id查找班级学生
      async getClassStudent(row) {
        this.reciteStudentList=[]
      queryClassStudents({ classId: row.classId }).then((resp) => {
        for (let i = 0; i < resp.data.length; i++) {
          // this.reciteStudentList = resp.data
          this.reciteStudentList.push({
            checkType:this.checkType,
            doneNum: 1,
            checkState: "0",
            remarks:'',
            studentId: resp.data[i].id,
            classId: resp.data[i].classId,
            studentName: resp.data[i].studentName,
            workDayId:row.id
          });
        }
        for(var i=0;i<this.selectStuList.length;i++){
            for(var j=0;j<this.reciteStudentList.length;j++){
                if(this.selectStuList[i].studentId==this.reciteStudentList[j].studentId){
                  this.reciteStudentList[j]=this.selectStuList[i]
                }
            }
          }
      });
    },
      getTableTitle(){
        this.getDicts("work_type").then(response => {
          this.tableTitle = response.data
        })
      },
      handleCloseDetail(){
        this.detailDialogVisible= false
      },
      handleWorkClose(){
        this.$confirm('你确定要关闭窗口吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.checkDialogVisible= false
          this.getList()
        }).catch(() => {
             
        });
      },
      handleClose(){
        this.$confirm('你确定要关闭窗口吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.dialogVisible= false
          this.getList()
        }).catch(() => {
             
        });
      },
      showWork(row){
        this.dialogVisible = true
        this.wordId = row.id
      },
      editToPage(row){
        this.$router.push({path:'/evaluation/day/edit', query:{id:row.id} })
      },
      /** 查询参数列表 */
      getList() {
        this.loading = true;
        pageList(this.queryParams).then(response => {
            this.deployList = response.data.list;
            this.total = response.data.total;
            this.loading = false;
          }
        );
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          paramName: undefined,
          paramKey: undefined,
          paramValue: undefined,
          paramType: "Y",
          remark: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handlebefore(file){
          console.log(file)
          let fileName = file.name
          let fileSize = file.size
          const maxSize = 1024*1024*2
          let fileNameIndex = fileName.lastIndexOf(".");
          let fileNameLength = fileName.length;
          let fileFormat = fileName.substring(fileNameIndex);
          debugger
          if(fileFormat.toLowerCase() != '.zip'){
              this.$message('只能上次zip文件,请重新选择');
              return false
          }
          if(maxSize < fileSize){
              this.$message('文件大于2MB,请重新选择');
              return false
          }
      },
      handleSuccess(){
          this.$message({
            message: '上传部署成功',
            type: 'success'
          });
          this.fileList=[]
          this.handleQuery()
      }
    }
  };
  </script>
  